package com.knowledge.web.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;


@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
    @Value("${app.swagger.enabled}")
    private boolean enabled;

    @Bean
    public Docket adminApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enabled)
                .groupName("service_api")
                .apiInfo(apiInfo())
//                .pathMapping("/swagger")
                .globalOperationParameters(
                        Arrays.asList(
                                new ParameterBuilder()
                                        .name("token")
                                        .description("登录token值")
                                        .modelRef(new ModelRef("string"))
                                        .parameterType("header")
                                        .build()
                        )
                )
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.knowledge.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * @desc 接口详情
     * @return
     */
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("接口文档")
                // 描述
                .description("文档接口信息")
                // 作者信息
                .contact(new Contact("Liguanh", null, null))
                // 版本
                .version("版本号:1.0" )
                .build();
    }
}
